var express = require('express');
var router = express.Router();
var addressToLocation = require('./map');
var { fosterCareModel, OrderModel } = require('../model/model');
/* GET home page. */
router.get('/searchAddress', async (req, res) => {
    let address = req.query.address
    try {
        const result = await addressToLocation({ address: address, key: 'RTOBZ-X4NCW-OS3RD-YDQUW-QQXWQ-TEF5D' });
        res.json(result); // 将结果返回给客户端
    } catch (error) {
        console.error('Error:', error);
        res.status(500).json({ error: 'Internal Server Error' });
    }
});



// 获取店铺信息
router.get('/getShopInfo', async (req, res) => {
    // 获取数据
    const data = await fosterCareModel.find()
    res.send({
        code: 200,
        msg: '请求成功',
        data
    })
})

router.get('/getDetail', async (req, res) => {
    const id = req.query.id;
    const data = await fosterCareModel.find({ id: id })
    res.send({
        code: 200,
        msg: '请求成功',
        data
    })
})


router.post('/pay', async (req, res) => {
    // 获取的前端数据
    const beforeList = req.body.data
    // 将数据存入数据库
    const data = {
        hostname: beforeList.hostname,
        img: beforeList.img[0],
        starttime: '2024-07-15',
        endtime: '2024-07-16',
        petnum: 2,
        price: beforeList.price,
        ordernum: 0,
        status: 0,
        trade_no: "",
        petname: "小牛奶"
    }
    // 添加到OrderModel数据库
    await OrderModel.create(data)
    res.send({
        code: 200,
        msg: '添加成功',
        data
    })
})
module.exports = router;